import numpy as np
import pandas as pd
import xgboost as xgb
import os
import time  #记录程序运行时间
import re
from write_in_mysql import update,inqury2

#遍历文件夹中所有文件
def scaner_file(url):
    file  = os.listdir(url)
    return file

#匹配文件名是否为ip.csv
def choosefile(filename):
    if re.match(r'\b(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))\b\.csv',filename) != None:
        return True
    else:
        return False

if __name__ == '__main__':
    start_time = time.time()
    #读入数据
    dir_name = os.path.dirname(__file__) + '/'
    url = os.path.abspath(os.path.dirname(__file__))
    file = scaner_file(url)
    l = len(file)
    ss_ip = []
    for i in range(l):
        if choosefile(file[i]):
            tests = pd.read_csv(dir_name+file[i]) 
            xgb_test = xgb.DMatrix(tests)
            model_xgb_2 = xgb.Booster()
            model_xgb_2.load_model(dir_name+"xgb_040.json")
            # print("跑到这里了model.predict")
            ypreds = model_xgb_2.predict(xgb_test,model_xgb_2.best_iteration)
            preds = model_xgb_2.predict(xgb_test,iteration_range=(0, model_xgb_2.best_iteration))
            # print(preds)
            # print(str(list(preds).count(1)/len(preds)*100)+"%")
            print(np.sum(preds==1)/len(preds))
            if np.sum(preds==1)/len(preds) >= 0.8:
                ss_ip.append(file[i][:-4])
    ss_num = len(ss_ip)
    print(ss_ip)
    print(len(ss_ip))
    update("update date1 set num = "+ str(ss_num) +" where name = 'shadowsocks';")
    sum = inqury2("select * from ip_num")
    no_ss_num = sum - ss_num
    print(no_ss_num)
    update("update date1 set num = "+ str(no_ss_num) +" where name = 'others';")

    #输出运行时长
    cost_time = time.time()-start_time
    print("xgboost success!",'\n',"cost time:",cost_time,"(s)")